package com.acme.bankapp.domain.bank;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.acme.exceptions.*;

public class Bank {
  private ArrayList<Client> clients = new ArrayList<Client>();
  private ArrayList<ClientRegistrationListener> listeners;
  public Bank(ArrayList<ClientRegistrationListener> listeners) throws IllegalArgumentException, 
              NotEnoughFundsException, ClientExistsException{
    this.listeners = listeners;
    class PrintClientListener implements ClientRegistrationListener{
      @Override
      public void onClientAdded(Client c){
        c.getClientSaltation();
      }
    }
    class EmailNotificationListener implements ClientRegistrationListener{
      @Override
      public void onClientAdded(Client c){
        System.out.println("Notification email for client " + c.getName() + " to be sent");
      }
    }
    class DebugListener implements ClientRegistrationListener{
      @Override
      public void onClientAdded(Client c){
        System.out.println(c.toString());
        System.out.println((new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS")).format(new Date()));
      }
    }
    listeners.add(new PrintClientListener());
    listeners.add(new EmailNotificationListener());
    listeners.add(new DebugListener());
    for(int i=0; i<10; i++){
      addClient(new Client("SomeName" + i, Gender.MALE));
    }
  }
  public ArrayList<Client> getClients(){
    return clients;
  }
  public void addClient(Client c) throws ClientExistsException{
    for(int i=0; i<clients.size(); i++){
      if(clients.get(i).getName() == c.getName()){
        throw new ClientExistsException();
      }
    }
    for(int i=0; i<listeners.size(); i++){
      listeners.get(i).onClientAdded(c);
    }
    clients.add(c);
  }
}
